package org.setaria.control
{
	import flash.display.InteractiveObject;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	import mx.managers.IFocusManager;
	import mx.managers.IFocusManagerComponent;
	
	import org.setaria.skins.control.TimeInputSkin;
	
	import spark.components.Button;
	import spark.components.TextInput;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class TimeInput extends SkinnableComponent implements IFocusManagerComponent
	{
		public function TimeInput()
		{
			super();
			this.setStyle("skinClass",TimeInputSkin)
//			this.focusEnabled = false;
		}
		
		
		private var _hour:int = 0;
		private var _minute:int=0;
		private var _second:int = 0;
		
		public function set hour(hour:int):void{
			if(hour>23)this._hour = 23;
			else if(hour<0)this._hour = 0;
			else this._hour = hour;
		}
		public function get hour():int{
			return this._hour;
		}
		public function set minute(minute:int):void{
			if(minute>59)this._minute = 59;
			else if(minute<0)this._minute = 0;
			else this._minute = minute;
		}
		public function get minute():int{
			return this._minute;
		}
		public function set second(second:int):void{
			if(second>59)this._second = 59;
			else if(second<0)this._second = 0;
			else this._second = second;
		}
		public function get second():int{
			return this._second;
		}
		
		/**
		 * 为传入的日期设置时间（小时，分钟，秒）
		 * @param date Date 需要设置时间的日期，如果为空则默认使用当前时间
		 * @return Date 
		 * */
		public function getDateTime(date:Date = null):Date{
			if(date == null){
				date = new Date();
			}
			date.hours = _hour;
			date.minutes = _minute;
			date.seconds = _second;
			return date;
		}
		
		/**
		 * 调整时间的步长
		 * */
		public var stepSize:int = 1;
		
		[SkinPart(required="false")]
		public var incrementButton:Button;
		[SkinPart(required="false")]
		public var decrementButton:Button;
		[SkinPart(required="false")]
		public var hourText:TextInput;
		[SkinPart(required="false")]
		public var minuteText:TextInput;
		[SkinPart(required="false")]
		public var secondText:TextInput;
		
		private var currentTi:TextInput;
		
		
		private function setHour(hour:int):void{
			_hour = hour;
			hourText.text = _hour.toString();
		}
		private function setMinute(minute:int):void{
			_minute = minute;
			minuteText.text = _minute.toString();
		}
		private function setSecond(second:int):void{
			_second = second;
			secondText.text = _second.toString();
		}
		
		override public function setFocus():void{
			if(!currentTi){
				currentTi = hourText;
			}
			this.focusManager.setFocus(currentTi);
				if (currentTi.editable || currentTi.selectable)
				{
					currentTi.selectAll();
				}
		}
		
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			if(instance == incrementButton){
				incrementButton.focusEnabled = false;
				incrementButton.addEventListener(FlexEvent.BUTTON_DOWN,incrementButton_buttonDownHandler);
				incrementButton.autoRepeat = true;
			}else if(instance == decrementButton){
				decrementButton.focusEnabled = false;
				decrementButton.addEventListener(FlexEvent.BUTTON_DOWN,decrementButton_buttonDownHandler);
				decrementButton.autoRepeat = true;
			}else if(instance == hourText){
				hourText.restrict = "0-9"
				hourText.maxChars = 2;
				hourText.addEventListener(FlexEvent.ENTER,hourText_enterHandler);
				hourText.addEventListener(FocusEvent.FOCUS_OUT,hourText_focusOutHandler);
				hourText.addEventListener(FocusEvent.FOCUS_IN,hourText_focusInHandler);
				hourText.text = _hour.toString();
			}else if(instance == minuteText){
				minuteText.restrict = "0-9"
				minuteText.maxChars = 2;
				minuteText.addEventListener(FlexEvent.ENTER,minuteText_enterHandler);
				minuteText.addEventListener(FocusEvent.FOCUS_OUT,minuteText_focusOutHandler);
				minuteText.addEventListener(FocusEvent.FOCUS_IN,minuteText_focusInHandler);
				minuteText.text = _minute.toString();
			}else if(instance == secondText){
				secondText.restrict = "0-9"
				secondText.maxChars = 2;
				secondText.addEventListener(FlexEvent.ENTER,secondText_enterHandler);
				secondText.addEventListener(FocusEvent.FOCUS_OUT,secondText_focusOutHandler);
				secondText.addEventListener(FocusEvent.FOCUS_IN,secondText_focusInHandler);
				secondText.text = _second.toString();
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void{
			if(instance == incrementButton){
				incrementButton.removeEventListener(FlexEvent.BUTTON_DOWN,incrementButton_buttonDownHandler);
			}else if(instance == decrementButton){
				decrementButton.removeEventListener(FlexEvent.BUTTON_DOWN,decrementButton_buttonDownHandler);
			}else if(instance == hourText){
				hourText.removeEventListener(FlexEvent.ENTER,hourText_enterHandler);
				hourText.removeEventListener(FocusEvent.FOCUS_OUT,hourText_focusOutHandler);
				hourText.removeEventListener(FocusEvent.FOCUS_IN,hourText_focusInHandler);
			}else if(instance == minuteText){
				minuteText.removeEventListener(FlexEvent.ENTER,minuteText_enterHandler);
				minuteText.removeEventListener(FocusEvent.FOCUS_OUT,minuteText_focusOutHandler);
				minuteText.removeEventListener(FocusEvent.FOCUS_IN,minuteText_focusInHandler);
			}else if(instance == secondText){
				secondText.removeEventListener(FlexEvent.ENTER,secondText_enterHandler);
				secondText.removeEventListener(FocusEvent.FOCUS_OUT,secondText_focusOutHandler);
				secondText.removeEventListener(FocusEvent.FOCUS_IN,secondText_focusInHandler);
			}
			super.partRemoved(partName,instance);
		}
		
		protected function incrementButton_buttonDownHandler(e:FlexEvent):void{
			if(currentTi == hourText){
				setHour(incrementChangeByStepSize(_hour,24));
			}else if(currentTi == minuteText){
				setMinute(incrementChangeByStepSize(_minute,60));
			}else if(currentTi == secondText){
				setSecond(incrementChangeByStepSize(_second,60));
			}
		}
		protected function decrementButton_buttonDownHandler(e:FlexEvent):void{
			if(currentTi == hourText){
				setHour(decrementChangeByStepSize(_hour,0));
			}else if(currentTi == minuteText){
				setMinute(decrementChangeByStepSize(_minute,0));
			}else if(currentTi == secondText){
				setSecond(decrementChangeByStepSize(_second,0));
			}
		}
		protected function hourText_enterHandler(e:FlexEvent):void{
			setHour(validityValue(TextInput(e.currentTarget).text,24));
		}
		protected function hourText_focusOutHandler(e:FocusEvent):void{
			setHour(validityValue(TextInput(e.currentTarget).text,24));
		}
		protected function hourText_focusInHandler(e:FocusEvent):void{
			currentTi = hourText;
			hourText.selectAll();
			focusInHandler(e);
		}
		protected function minuteText_enterHandler(e:FlexEvent):void{
			setMinute(validityValue(TextInput(e.currentTarget).text,60));
		}
		protected function minuteText_focusOutHandler(e:FocusEvent):void{
			setMinute(validityValue(TextInput(e.currentTarget).text,60));
		}
		protected function minuteText_focusInHandler(e:FocusEvent):void{
			currentTi = minuteText;
			minuteText.selectAll();
			focusInHandler(e);
		}
		protected function secondText_enterHandler(e:FlexEvent):void{
			setSecond(validityValue(TextInput(e.currentTarget).text,60));
		}
		protected function secondText_focusOutHandler(e:FocusEvent):void{
			setSecond(validityValue(TextInput(e.currentTarget).text,60));
		}
		protected function secondText_focusInHandler(e:FocusEvent):void{
			currentTi = secondText;
			secondText.selectAll();
			focusInHandler(e);
		}
		override protected function focusInHandler(event:FocusEvent):void
		{
			super.focusInHandler(event);
			
			var fm:IFocusManager = focusManager;
			if (fm)
				fm.defaultButtonEnabled = false;
		}
		
		/**
		 * @param valueStr String 要验证的字符串值
		 * @param type int 进制 值为 24 或 60
		 * */
		private function validityValue(valueStr:String,type:int=60):int{
			var value:int = 0;
			if(valueStr== null || valueStr == ""){
				return value;
			}
			value = parseInt(valueStr);
			if(value>type-1)return type-1;
			else return value;
		}
		
		private function incrementChangeByStepSize(value:int,type:int):int{
			return value >=type-1 ? type-1:value+stepSize;			
		}
		private function decrementChangeByStepSize(value:int,type:int):int{
			return value <= type ? type:value-stepSize;			
		}
	}
}